package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.UserFollow;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.service.UserFollowService;
import com.atguigu.vo.UserFollowVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/userFollow")
public class UserFollowController {
    @Reference
    private UserFollowService userFollowService;

    @RequestMapping("/auth/follow/{houseId}")
    public Result follow(@PathVariable Long houseId, HttpServletRequest request){
        //直接insert就行,但我们得先从session中获取到用户信息拿到用户id
       UserInfo userInfo  = (UserInfo)request.getSession().getAttribute("USER");
        //封装数据到UserFollow对象中
        UserFollow userFollow = new UserFollow();
        userFollow.setUserId(userInfo.getId());
        userFollow.setHouseId(houseId);
        //添加到数据库
        userFollowService.insert(userFollow);
        return Result.ok();
    }

    @RequestMapping("/auth/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable int pageNum,@PathVariable int pageSize,HttpServletRequest request){
        //分页查询我的关注  封装成pageInfo对象返回给前端
        //获取当前对象信息
        UserInfo userInfo =(UserInfo) request.getSession().getAttribute("USER");
        PageInfo<UserFollowVo> pageInfo = userFollowService.findListPage(pageNum,pageSize,userInfo.getId());
        return Result.ok(pageInfo);
    }

    @RequestMapping("/auth/cancelFollow/{id}")
    public Result cancelFollow(@PathVariable Long id){
        //根据传入的userfollow表的主键去取消关注,删除掉关注
        userFollowService.cancelFollow(id);
        return Result.ok();
    }
}
